package com.tjp.eight.ui.fragment.tabFind;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.subdetails.ActivitySubDetail;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.CircleImageView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/14.
 */

public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<FindResultModel.FindItemModel> arrayList;
    private Context context;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private View mHeaderView;

    FindAdapter(Context context) {
        this.context = context;
        arrayList = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && mHeaderView != null) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new FindViewHolder(mHeaderView);
        View layout = LayoutInflater.from(context).inflate(R.layout.fragment_find_item, parent, false);
        return new FindViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        int pos = 0;
        if (mHeaderView != null) {
            pos = position - 1;
        } else {
            pos = position;
        }
        final String caid = arrayList.get(pos).caid;
        final String cid = arrayList.get(pos).cid;
        final String title = arrayList.get(pos).cat_name;
        ((FindViewHolder) holder).rl_find_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入详细订阅分类
                ActivityUtils.startSubDetails(context, title, caid);
            }
        });

        ((FindViewHolder) holder).rl_find_sub_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入详细文章
                ActivityUtils.startDetails(context, cid);
            }
        });

        Glide.with(context).load(arrayList.get(pos).cat_cover_path).error(R.drawable.loading).into(((FindViewHolder) holder).civ_icon_find);
        Glide.with(context).load(arrayList.get(pos).cover_path).error(R.drawable.loading).into(((FindViewHolder) holder).iv_sub_pic);
        ((FindViewHolder) holder).tv_find_item_title.setText("" + arrayList.get(pos).title);
        ((FindViewHolder) holder).tv_find_name.setText("" + arrayList.get(pos).cat_name);
        ((FindViewHolder) holder).tv_sub_views.setText("" + StringUtils.toWan(arrayList.get(pos).views_all));
        ((FindViewHolder) holder).tv_sub_count.setText("" + StringUtils.toWan(arrayList.get(pos).subscibe));//订阅数

    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? arrayList.size() : arrayList.size() + 1;
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public void addData(ArrayList<FindResultModel.FindItemModel> findItemModels) {
        if (findItemModels == null || findItemModels.isEmpty()) return;
        for (FindResultModel.FindItemModel findItemModel : findItemModels) {
            arrayList.add(findItemModel);
        }
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();
    }

    public void refreshData(ArrayList<FindResultModel.FindItemModel> findItemModels) {
        if (findItemModels == null || findItemModels.isEmpty()) return;
        arrayList.clear();
        for (FindResultModel.FindItemModel findItemModel : findItemModels) {
            arrayList.add(findItemModel);
        }
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    class FindViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rl_find_sub;
        RelativeLayout rl_find_sub_detail;
        CircleImageView civ_icon_find;
        ImageView iv_sub_pic;
        TextView tv_find_name;
        TextView tv_find_item_title;
        TextView tv_sub_views;
        TextView tv_sub_count;

        public FindViewHolder(View itemView) {
            super(itemView);
            rl_find_sub = (RelativeLayout) itemView.findViewById(R.id.rl_find_sub);
            rl_find_sub_detail = (RelativeLayout) itemView.findViewById(R.id.rl_find_sub_detail);
            civ_icon_find = (CircleImageView) itemView.findViewById(R.id.civ_icon_find);
            iv_sub_pic = (ImageView) itemView.findViewById(R.id.iv_sub_pic);
            tv_find_name = (TextView) itemView.findViewById(R.id.tv_find_name);
            tv_find_item_title = (TextView) itemView.findViewById(R.id.tv_find_item_title);
            tv_sub_views = (TextView) itemView.findViewById(R.id.tv_sub_views);
            tv_sub_count = (TextView) itemView.findViewById(R.id.tv_sub_count);
        }
    }

}
